package org.zzh.controller.cargo.contractProduct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.zzh.controller.BaseController;
import org.zzh.domain.cargo.contractProduct.CoContractProduct;
import org.zzh.service.cargo.contractProduct.CoContractProductService;
import org.zzh.service.cargo.factory.CoFactoryService;
import org.zzh.service.common.CommonService;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;

@Controller
@RequestMapping("/cargo/contractProduct")
public class CoContractProductController extends BaseController {
    @Autowired
    public CoContractProductService coContractProductService;

    @Autowired
    public CoFactoryService coFactoryService;

    @Autowired
    private CommonService commonService;

    @RequestMapping(value = "/list.do")
    public ModelAndView list(
            @RequestParam(required = false, value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(required = false, value = "name", defaultValue = "") String name,
            @RequestParam(required = false, value = "contractId") String contractId
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageBean", coContractProductService.list(page, pageSize, contractId));
        modelAndView.addObject("contractId", contractId);
        modelAndView.addObject("factoryList", coFactoryService.list());
        modelAndView.setViewName("/WEB-INF/pages/cargo/product/product-list");
        return modelAndView;
    }

    @RequestMapping(value = "/delete.do")
    public String delete(String id, String contractId) {
        coContractProductService.deleteById(id);
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }

    @RequestMapping(value = "/toUpdate.do")
    public ModelAndView toUpdate(String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("contractProduct", coContractProductService.findById(id));
        modelAndView.addObject("factoryList", coFactoryService.list());
        modelAndView.setViewName("/WEB-INF/pages/cargo/product/product-update");
        return modelAndView;
    }

    @RequestMapping(value = "/edit.do")
    public String edit(@ModelAttribute CoContractProduct contractProduct, @RequestParam("productPhoto") MultipartFile productPhoto) {
        coContractProductService.edit(contractProduct);
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractProduct.getContractId();
    }

    @RequestMapping(value = "/toImport.do")
    public ModelAndView toImport(String contractId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("contractId", contractId);
        modelAndView.setViewName("/WEB-INF/pages/cargo/product/product-import");
        return modelAndView;
    }

    @RequestMapping(value = "/import.do")
    public String importExcel(String contractId, @RequestParam("file") MultipartFile file) {
        List<CoContractProduct> coContractProducts = commonService.importExcel(file, CoContractProduct.class);
        coContractProductService.addCoContractProductList(coContractProducts, contractId);
        return "redirect:/cargo/contract/list.do";
    }


}
